package filePackets;
import java.io.*; 
import java.net.*; 


/** 
 * Wrapper to receive Objects through a datagram socket. You can use this to receive 
 * any serializable object through a DatagramSocket instead of a TCP Socket. 
 */ 

 public class Receiver 
 { 

	 /** 
	  * Read an object from a datagram socket. 
	  *@param dSock the socket where the object should be received. 
	  */ 

	 public Object ReadObjectFrom(DatagramSocket dSock) throws IOException 
	 {

		 try {

			 byte[] recvBuf = new byte[5000]; 
			 DatagramPacket packet = new DatagramPacket(recvBuf, recvBuf.length); 
			 dSock.receive(packet); 
			 int byteCount = packet.getLength(); 
			 ByteArrayInputStream byteStream = new ByteArrayInputStream(recvBuf); 
			 ObjectInputStream is = new ObjectInputStream(new BufferedInputStream(byteStream)); 
			 Object o = is.readObject(); 

			 is.close(); 
			 return o; 

		 } catch (ClassNotFoundException e){ e.printStackTrace(); } 

		 return null; 

	 } 

 } 